import Mock from "mockjs"
const Random = Mock.Random
// 获取url的参数
function getUrlParams(url, name) {
    if (url.indexOf("?" > -1)) {
        let paramsAry = url.substring(url.indexOf("?") + 1).split("&")
        // 循环数据
        for (let i = 0; i < paramsAry.length; i++) {
            let tmp = paramsAry[i].split("=")
            // if params===name return val
            if (tmp[0] === name) {
                return tmp[1]
            }
        }
        return -1
    } else {
        return -1
    }
}
if (process.env.NODE_ENV == "development") {

    Mock.mock("/a/o/captcha", "get", {
        code: 300,
        message: "success",
        data: {
            captcha: Random.image('200x100', '#50B347', '#FFF', 'Mock.js')
        }
    })

    Mock.mock("/a/o/login", "post", {
        code: 300,
        message: "success",
        data: {
            id: Random.id(),
            username: Mock.mock("@cname()"),
            token: Mock.mock('@string(5)')
        }
    })

    Mock.mock("/a/o/token", "get", {
        code: 300,
        message: "success",
        data: {
            id: Random.id(),
            username: Mock.mock("@cname()"),
        }
    })

    Mock.mock("/a/o/logout", "get", {
        code: 300,
        message: "success",
    })

    const customer = {

    }
    Mock.mock(RegExp("/a/o/customer-list" + ".*"), "get", (options) => {
        // 当前页
        let pageNum = parseInt(getUrlParams(options.url, "pageNum"))
        if (pageNum == -1) {
            pageNum = 1
        }
        // 总条数
        let total = 20
        let pageSize = parseInt(getUrlParams(options.url, "pageSize")) == "" ? 10 : parseInt(getUrlParams(options.url, "pageSize"))

        let sortColumn = getUrlParams(options.url, "sortColumn")
        let isAse = getUrlParams(options.url, "isAse") ? getUrlParams(options.url, "isAse") : false

        let pages = Math.ceil(total / pageSize) //共多少页
        let startIndex = (pageNum - 1) * pageSize //开始下标
        let endIndex = (startIndex + pageSize) < total ? (startIndex + pageSize) : total //结束下标
        // let contactName = getUrlParams(options.url, "contactName")
        // let address = getUrlParams(options.url, "address")
        // let type = getUrlParams(options.url, "type")

        let customerList = []
        for (; startIndex < endIndex; startIndex++) {
            let customer = Mock.mock({
                id: "@id",
                contactName: "@cname",
                phone: Mock.mock(RegExp(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/)),
                "type|1": ["普通用户", "vip用户"],
                address: '@county(true)',
                "balance|1-100.1-2": 1,
                createTime: '@datetime()',
                vipExpiration: '@datetime()'
            })
            customerList.push(customer)
        }
        return {
            code: 300,
            message: "success",
            data: {
                customerList: customerList,
                total: total
            },
            pages: pages
        }
    })
}